---
title: "Replication File"
author: "Sarah Jacobs and Julian Morimoto"
date: "12/14/2020"
output: html_document
---
## Prerequisites
```{r}
library(tidyverse)
library(Matching)
library(cem)
library(MatchIt)
library(MatchingFrontier)
library(StatMatch)
library(broom)
library(readbitmap)
library(imager)
#Set the working directory
setwd("___")
```
## Matching:

```{r}
# load dataset"
df <- read.csv("Data4.csv")

#Create the bins for CEM 
bv2x_gender <- c(0, 1.5)
bv2x_liberal <- c(0, 1.5)
bdiplomat <- c(-1, 1.5)
bleft  <-c(-1, 0.5, 1.5)
bright <- c(-1, 0.5, 1.5)
breligious <- c(-1, 1.5)
bnationalist <- c(-1, 1.5) 
blegor_uk <- c(-1, 1.5)
blegor_so <- c(-1, 0.5, 1.5)
blegor_fr <- c(-1, 1.5) 
bappointmentyear <- c(0, 1989, 1998, 2004, 2021)
  
#Run the Coarsened Exact Matching Analysis 
newmatch <- cem(treatment = "female", 
    data = df, 
    cutpoints = list(appointmentyear = bappointmentyear, diplomat = bdiplomat, 
                     left = bleft, legor_fr = blegor_fr, legor_so = blegor_so, 
                     legor_uk = blegor_uk, nationalist = bnationalist, 
                     religious = breligious, right = bright, v2x_gender=bv2x_gender,
                     v2x_liberal = bv2x_liberal),
    drop = c("filename", "activist", "progov",
                                              "lastname", "singleapplicant", 
                                              "femaleapplicant", "prisoner", 
                                              "physical", "liberty", "fairtrial",
                                              "discrimination", "privacy", "speech", 
                                              "property", "maleapplicant",
                                              "judgeid"),
    keep.all = TRUE)

save(newmatch, file = "CEM.RData")
```

## Finding CEM imbalance, and comparing it with Genetic Matching Imbalance

```{r}
## Create matched dataset 
covars <- c("newid", "diplomat", "v2x_gender", "v2x_liberal", "left", "right","religious", "nationalist", "legor_uk", "legor_so",  "legor_fr", "appointmentyear")

matched_data <- rbind(df[which(newmatch$matched == 1 & df$female == 0), c(covars, "female")] * newmatch$w[which(newmatch$matched == 1 & df$female == 0)],
                      df[which(newmatch$matched == 1 & df$female == 1), c(covars, "female")] * newmatch$w[which(newmatch$matched == 1 & df$female == 1)])

## Checking CEM balance 

cov_means_matched <- matched_data %>% 
  group_by(female) %>% 
  summarise_at(covars, mean) %>%
  t() %>%
  as_tibble(rownames = 'var') %>%
  slice(-1) %>%
  rename(mean_control = 2,
         mean_treated = 3)


cov_var_matched <- matched_data %>% 
  group_by(female) %>% 
  summarise_at(covars, var) %>%
  t() %>%
  as_tibble(rownames = 'var') %>%
  slice(-1) %>%
  rename(var_control = 2,
         var_treated = 3)

bal_post <- cov_means_matched %>% 
  left_join(cov_var_matched) %>%
  mutate(mean_diff = mean_treated - mean_control,
         SMD = mean_diff / sqrt((var_control + var_treated)/2)) %>%
  mutate(type = 'CEM')

##Genetic Matching Balance
#load the genetic matching dataset
gmatches <- read.csv("MatchedDataJan4final.csv")

## Means in treated / control group 

cov_means <- gmatches %>% 
  group_by(female) %>% 
  summarise_at(covars, mean) %>%
  t() %>%
  as_tibble(rownames = 'var') %>%
  slice(-1) %>%
  rename(mean_control = 2,
         mean_treated = 3)

## Variance in treated / control group 

cov_var <- gmatches %>% 
  group_by(female) %>% 
  summarise_at(covars, var) %>%
  t() %>%
  as_tibble(rownames = 'var') %>%
  slice(-1) %>%
  rename(var_control = 2,
         var_treated = 3)

#genetic balance match
bal_pre <- cov_means %>% 
  left_join(cov_var) %>%
  mutate(mean_diff = mean_treated - mean_control,
         SMD = mean_diff / sqrt((var_control + var_treated)/2)) %>%
  mutate(type = 'Genetic Matching')

## Compare balance between Genetic Matching and CEM

ggplot(rbind(bal_pre, bal_post), aes(x = var, y = SMD, 
                                     col = type, group = type, shape = type)) +
  geom_point() + 
  coord_flip() + 
  labs(y = 'Normalized difference',
       x = 'Variable') + 
  theme_bw() + 
  geom_hline(yintercept = 0,
             linetype = 'dotted') + 
  theme(legend.position = 'bottom',
        legend.title = element_blank())
```

## Mean Normalized Difference Metrics for Genetic Matching and CEM
```{r}
#Genetic
abs(mean(bal_pre$SMD))

#CEM
abs(mean(bal_post$SMD))
```

## Number of Observations Pruned and Matched
```{r}
newmatch
```

## Coefficient Generation and Plotting for CEM
```{r}
#Regression analysis

#(re)load data and MatchIt object
df <- read.csv("Data4.csv")
load("CEM.RData")

#Add panel stats to dataframe
panel<-read.csv("Panelvotes.csv")
df<-left_join(df, panel, by = "newid")
df$voteprop <- (df$Njudgesproresp-df$activist)/df$Njudges

df$weight  <- newmatch$w
cem.matches <- df[which(df$weight != 0),]

mod1 <- lm(activist ~ 
             female + voteprop, 
           data = cem.matches,
           weights = weight)
summary(mod1)

mod1.tidy <- tidy(mod1)
mod1.tidy$description <- "All Judgments"
mod1.tidy$applicants<- "All applicants"

mod1b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, femaleapplicant==1),
            weights = weight)
mod1b.tidy <- tidy(mod1b)
mod1b.tidy$applicants<- "Female"
mod1b.tidy$description <- "All Judgments"

mod2 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, physical==1),
           weights = weight)
mod2.tidy <- tidy(mod2)
mod2.tidy$description <- "Physical integrity rights"
mod2.tidy$applicants<- "All applicants"

mod2b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, physical==1 & femaleapplicant==1),
            weights = weight)
mod2b.tidy <- tidy(mod2b)
mod2b.tidy$applicants <- "Female"
mod2b.tidy$description <- "Physical integrity rights"

mod3 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, liberty==1),
           weights = weight)
mod3.tidy <- tidy(mod3)
mod3.tidy$description <- "Liberty"
mod3.tidy$applicants<- "All applicants"

mod3b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, liberty==1 & femaleapplicant==1),
            weights = weight)
mod3b.tidy <- tidy(mod3b)
mod3b.tidy$description <- "Liberty"
mod3b.tidy$applicants <- "Female"

mod4 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, prisoner==1),
           weights = weight)
mod4.tidy <- tidy(mod4)
mod4.tidy$description <- "Imprisoned applicant"
mod4.tidy$applicants<- "All applicants"

mod4b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, prisoner==1  & femaleapplicant==1),
            weights = weight)
mod4b.tidy <- tidy(mod4b)
mod4b.tidy$applicants <- "Female"
mod4b.tidy$description <- "Imprisoned applicant"

mod5 <- lm(activist ~ 
             voteprop + female, 
           data = filter(cem.matches,property==1),
           weights = weight)
mod5.tidy <- tidy(mod5)
mod5.tidy$description <- "Property"
mod5.tidy$applicants<- "All applicants"

mod5b <- lm(activist ~ 
              voteprop + female, 
            data = filter(cem.matches,property==1  & femaleapplicant==1),
            weights = weight)
mod5b.tidy <- tidy(mod5b)
mod5b.tidy$description <- "Property"
mod5b.tidy$applicants <- "Female"

mod6 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, speech==1),
           weights = weight)
mod6.tidy <- tidy(mod6)
mod6.tidy$description <- "Free Speech"
mod6.tidy$applicants<- "All applicants"


mod6b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, speech==1 & femaleapplicant==1),
            weights = weight)
mod6b.tidy <- tidy(mod6b)
mod6b.tidy$description <- "Free Speech"
mod6b.tidy$applicants <- "Female"

mod7 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, fairtrial==1),
           weights = weight)
mod7.tidy <- tidy(mod7)
mod7.tidy$description <- "Fair Trial"
mod7.tidy$applicants<- "All applicants"

mod7b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, fairtrial==1 & femaleapplicant==1),
            weights = weight)
mod7b.tidy <- tidy(mod7b)
mod7b.tidy$description <- "Fair Trial"
mod7b.tidy$applicants <- "Female"

mod8 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, discrimination==1),
           weights = weight)
mod8.tidy <- tidy(mod8)
mod8.tidy$description <- "Discrimination"
mod8.tidy$applicants<- "All applicants"

mod8b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, discrimination==1 & femaleapplicant==1),
            weights = weight)
mod8b.tidy <- tidy(mod8b)
mod8b.tidy$description <- "Discrimination"
mod8b.tidy$applicants <- "Female"


mod9 <- lm(activist ~ 
             female + voteprop, 
           data = filter(cem.matches, privacy==1),
           weights = weight)
mod9.tidy <- tidy(mod9)
mod9.tidy$description <- "Privacy"
mod9.tidy$applicants<- "All applicants"

mod9b <- lm(activist ~ 
              female + voteprop, 
            data = filter(cem.matches, privacy==1 & femaleapplicant==1),
            weights = weight)
mod9b.tidy <- tidy(mod9b)
mod9b.tidy$description <- "Privacy"
mod9b.tidy$applicants <- "Female"


Coef <- bind_rows(mod1.tidy,mod2.tidy,mod3.tidy,mod4.tidy,mod5.tidy,mod6.tidy,mod7.tidy,mod8.tidy,mod9.tidy,
                  mod1b.tidy,mod2b.tidy,mod3b.tidy,mod4b.tidy,mod5b.tidy,mod6b.tidy,mod7b.tidy,mod8b.tidy,mod9b.tidy )

Coef$conf.low <- Coef$estimate - 1.96*abs(Coef$std.error)
Coef$conf.high <-Coef$estimate + 1.96*abs(Coef$std.error)

FemaleCoef <- filter(Coef,term=="female")

FemaleCoef$term<-FemaleCoef$description
FemaleCoef$model<-FemaleCoef$applicants
library(dotwhisker)
dwplot(FemaleCoef)


coefplot <- dwplot(FemaleCoef, 
                   # here are our regular aesthetics
                   dot_args = list(aes(colour = model, 
                                       shape = model)), 
                   size = 3) + 
  theme_bw() + 
  labs(title = "Effect of Female Judge on Probability of Violation", 
       x = "Marginal Effect with 95% CIs", 
       y = "Issue") +
  theme(plot.title = element_text(face="bold"),
        legend.position = "bottom",
        legend.background = element_rect(colour="grey80"),
        legend.title.align = .5) +
  scale_shape_discrete(name  ="Models", breaks = c(0, 1)) + # breaks assign shapes
  scale_colour_grey(start = .3, end = .7, name = "Models") # start/end for light/dark greys

png("Coeffigure1Jan4.png",  width = 6, height = 8, units = 'in', res = 300)
coefplot
dev.off()

#Based on full regression models

mod1 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear  , 
           data = cem.matches,
           weights = weight)
summary(mod1)

mod1.tidy <- tidy(mod1)
mod1.tidy$description <- "All Judgments"
mod1.tidy$applicants<- "All applicants"

mod1b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, femaleapplicant==1),
            weights = weight)
mod1b.tidy <- tidy(mod1b)
mod1b.tidy$applicants<- "Female"
mod1b.tidy$description <- "All Judgments"

mod1c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, maleapplicant==1),
            weights = weight)
mod1c.tidy <- tidy(mod1c)
mod1c.tidy$applicants<- "Male"
mod1c.tidy$description <- "All Judgments"

mod2 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, physical==1),
           weights = weight)
mod2.tidy <- tidy(mod2)
mod2.tidy$description <- "Physical integrity rights"
mod2.tidy$applicants<- "All applicants"

mod2b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, physical==1 & femaleapplicant==1),
            weights = weight)
mod2b.tidy <- tidy(mod2b)
mod2b.tidy$applicants <- "Female"
mod2b.tidy$description <- "Physical integrity rights"

mod2c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, physical==1 & maleapplicant==1),
            weights = weight)
mod2c.tidy <- tidy(mod2c)
mod2c.tidy$applicants <- "Male"
mod2c.tidy$description <- "Physical integrity rights"

mod3 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, liberty==1),
           weights = weight)
mod3.tidy <- tidy(mod3)
mod3.tidy$description <- "Liberty"
mod3.tidy$applicants<- "All applicants"

mod3b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, liberty==1 & femaleapplicant==1),
            weights = weight)
mod3b.tidy <- tidy(mod3b)
mod3b.tidy$description <- "Liberty"
mod3b.tidy$applicants <- "Female"

mod3c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, liberty==1 & maleapplicant==1),
            weights = weight)
mod3c.tidy <- tidy(mod3c)
mod3c.tidy$description <- "Liberty"
mod3c.tidy$applicants <- "Male"

mod4 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, prisoner==1),
           weights = weight)
mod4.tidy <- tidy(mod4)
mod4.tidy$description <- "Imprisoned applicant"
mod4.tidy$applicants<- "All applicants"

mod4b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, prisoner==1  & femaleapplicant==1),
            weights = weight)
mod4b.tidy <- tidy(mod4b)
mod4b.tidy$applicants <- "Female"
mod4b.tidy$description <- "Imprisoned applicant"

mod4c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, prisoner==1  & maleapplicant==1),
            weights = weight)
mod4c.tidy <- tidy(mod4c)
mod4c.tidy$applicants <- "Male"
mod4c.tidy$description <- "Imprisoned applicant"

mod5 <- lm(activist ~ 
             voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches,property==1),
           weights = weight)
mod5.tidy <- tidy(mod5)
mod5.tidy$description <- "Property"
mod5.tidy$applicants<- "All applicants"

mod5b <- lm(activist ~ 
              voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches,property==1  & femaleapplicant==1),
            weights = weight)
mod5b.tidy <- tidy(mod5b)
mod5b.tidy$description <- "Property"
mod5b.tidy$applicants <- "Female"

mod5c <- lm(activist ~ 
              voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches,property==1  & maleapplicant==1),
            weights = weight)
mod5c.tidy <- tidy(mod5c)
mod5c.tidy$description <- "Property"
mod5c.tidy$applicants <- "Male"

mod6 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, speech==1),
           weights = weight)
mod6.tidy <- tidy(mod6)
mod6.tidy$description <- "Free Speech"
mod6.tidy$applicants<- "All applicants"


mod6b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, speech==1 & femaleapplicant==1),
            weights = weight)
mod6b.tidy <- tidy(mod6b)
mod6b.tidy$description <- "Free Speech"
mod6b.tidy$applicants <- "Female"

mod6c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, speech==1 & maleapplicant==1),
            weights = weight)
mod6c.tidy <- tidy(mod6c)
mod6c.tidy$description <- "Free Speech"
mod6c.tidy$applicants <- "Male"

mod7 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, fairtrial==1),
           weights = weight)
mod7.tidy <- tidy(mod7)
mod7.tidy$description <- "Fair Trial"
mod7.tidy$applicants<- "All applicants"

mod7b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, fairtrial==1 & femaleapplicant==1),
            weights = weight)
mod7b.tidy <- tidy(mod7b)
mod7b.tidy$description <- "Fair Trial"
mod7b.tidy$applicants <- "Female"

mod7c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, fairtrial==1 & maleapplicant==1),
            weights = weight)
mod7c.tidy <- tidy(mod7c)
mod7c.tidy$description <- "Fair Trial"
mod7c.tidy$applicants <- "Male"


mod8 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, discrimination==1),
           weights = weight)
mod8.tidy <- tidy(mod8)
mod8.tidy$description <- "Discrimination"
mod8.tidy$applicants<- "All applicants"

mod8b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, discrimination==1 & femaleapplicant==1),
            weights = weight)
mod8b.tidy <- tidy(mod8b)
mod8b.tidy$description <- "Discrimination"
mod8b.tidy$applicants <- "Female"

mod8c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, discrimination==1 & maleapplicant==1),
            weights = weight)
mod8c.tidy <- tidy(mod8c)
mod8c.tidy$description <- "Discrimination"
mod8c.tidy$applicants <- "Male"


mod9 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(cem.matches, privacy==1),
           weights = weight)
mod9.tidy <- tidy(mod9)
mod9.tidy$description <- "Privacy"
mod9.tidy$applicants<- "All applicants"

mod9b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, privacy==1 & femaleapplicant==1),
            weights = weight)
mod9b.tidy <- tidy(mod9b)
mod9b.tidy$description <- "Privacy"
mod9b.tidy$applicants <- "Female"

mod9c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(cem.matches, privacy==1 & maleapplicant==1),
            weights = weight)
mod9c.tidy <- tidy(mod9c)
mod9c.tidy$description <- "Privacy"
mod9c.tidy$applicants <- "Male"


Coef <- bind_rows(mod1.tidy,mod2.tidy,mod3.tidy,mod4.tidy,mod5.tidy,mod6.tidy,mod7.tidy,mod8.tidy,mod9.tidy,
                  mod1b.tidy,mod2b.tidy,mod3b.tidy,mod4b.tidy,mod5b.tidy,mod6b.tidy,mod7b.tidy,mod8b.tidy,mod9b.tidy,
                  mod1c.tidy,mod2c.tidy,mod3c.tidy,mod4c.tidy,mod5c.tidy,mod6c.tidy,mod7c.tidy,mod8c.tidy,mod9c.tidy)

Coef$conf.low <- Coef$estimate - 1.96*abs(Coef$std.error)
Coef$conf.high <-Coef$estimate + 1.96*abs(Coef$std.error)

write.csv(Coef,"CoefficientsGeneticMatchingJan4MB.csv")

FemaleCoef <- filter(Coef,term=="female")

FemaleCoef$term<-FemaleCoef$description
FemaleCoef$model<-FemaleCoef$applicants
library(dotwhisker)
dwplot(FemaleCoef)

Coef$model<-Coef$description


coefplot <- dwplot(FemaleCoef, 
                   # here are our regular aesthetics
                   dot_args = list(aes(colour = model, 
                                       shape = model)), xlim = c(-0.5, 100),
                   size = 3) + 
  theme_light() + scale_colour_manual(values=c("black", "darkgray","gray"))+
  scale_size_manual(values=c(8,8,8)) +
  labs(title = "Coarsened Exact Matching (CEM)", 
       x = "Marginal Effect with 95% CIs", 
       y = "Issue") +
  theme(plot.title = element_text(face="bold"),
        legend.position = "bottom",
        legend.background = element_rect(colour="grey80"),
        legend.title.align = .5) + geom_vline(xintercept = 0) + xlim(-0.5, 1)
  

coefplot

png("Coeffigure2Jan4final.png",  width = 6, height = 8, units = 'in', res = 300)
coefplot
dev.off()
```

##Finding Genetic Matching Coefficients Based on Genetically Matched Data
```{r}
#Regression analysis
library(tidyverse)
library(broom)

#load data and MatchIt object
df <- read.csv("Data4.csv")

genetic.matches <- read.csv("MatchedDataJan4final.csv")


#Minimal Regression Models

mod1 <- lm(activist ~ 
             female + voteprop, 
           data = genetic.matches,
           weights = weight)
summary(mod1)

mod1.tidy <- tidy(mod1)
mod1.tidy$description <- "All Judgments"
mod1.tidy$applicants<- "All applicants"

mod1b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, femaleapplicant==1),
            weights = weight)
mod1b.tidy <- tidy(mod1b)
mod1b.tidy$applicants<- "Female"
mod1b.tidy$description <- "All Judgments"

mod2 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, physical==1),
           weights = weight)
mod2.tidy <- tidy(mod2)
mod2.tidy$description <- "Physical integrity rights"
mod2.tidy$applicants<- "All applicants"

mod2b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, physical==1 & femaleapplicant==1),
            weights = weight)
mod2b.tidy <- tidy(mod2b)
mod2b.tidy$applicants <- "Female"
mod2b.tidy$description <- "Physical integrity rights"

mod3 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, liberty==1),
           weights = weight)
mod3.tidy <- tidy(mod3)
mod3.tidy$description <- "Liberty"
mod3.tidy$applicants<- "All applicants"

mod3b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, liberty==1 & femaleapplicant==1),
            weights = weight)
mod3b.tidy <- tidy(mod3b)
mod3b.tidy$description <- "Liberty"
mod3b.tidy$applicants <- "Female"

mod4 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, prisoner==1),
           weights = weight)
mod4.tidy <- tidy(mod4)
mod4.tidy$description <- "Imprisoned applicant"
mod4.tidy$applicants<- "All applicants"

mod4b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, prisoner==1  & femaleapplicant==1),
            weights = weight)
mod4b.tidy <- tidy(mod4b)
mod4b.tidy$applicants <- "Female"
mod4b.tidy$description <- "Imprisoned applicant"

mod5 <- lm(activist ~ 
             voteprop + female, 
           data = filter(genetic.matches,property==1),
           weights = weight)
mod5.tidy <- tidy(mod5)
mod5.tidy$description <- "Property"
mod5.tidy$applicants<- "All applicants"

mod5b <- lm(activist ~ 
              voteprop + female, 
            data = filter(genetic.matches,property==1  & femaleapplicant==1),
            weights = weight)
mod5b.tidy <- tidy(mod5b)
mod5b.tidy$description <- "Property"
mod5b.tidy$applicants <- "Female"

mod6 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, speech==1),
           weights = weight)
mod6.tidy <- tidy(mod6)
mod6.tidy$description <- "Free Speech"
mod6.tidy$applicants<- "All applicants"


mod6b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, speech==1 & femaleapplicant==1),
            weights = weight)
mod6b.tidy <- tidy(mod6b)
mod6b.tidy$description <- "Free Speech"
mod6b.tidy$applicants <- "Female"

mod7 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, fairtrial==1),
           weights = weight)
mod7.tidy <- tidy(mod7)
mod7.tidy$description <- "Fair Trial"
mod7.tidy$applicants<- "All applicants"

mod7b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, fairtrial==1 & femaleapplicant==1),
            weights = weight)
mod7b.tidy <- tidy(mod7b)
mod7b.tidy$description <- "Fair Trial"
mod7b.tidy$applicants <- "Female"

mod8 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, discrimination==1),
           weights = weight)
mod8.tidy <- tidy(mod8)
mod8.tidy$description <- "Discrimination"
mod8.tidy$applicants<- "All applicants"

mod8b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, discrimination==1 & femaleapplicant==1),
            weights = weight)
mod8b.tidy <- tidy(mod8b)
mod8b.tidy$description <- "Discrimination"
mod8b.tidy$applicants <- "Female"


mod9 <- lm(activist ~ 
             female + voteprop, 
           data = filter(genetic.matches, privacy==1),
           weights = weight)
mod9.tidy <- tidy(mod9)
mod9.tidy$description <- "Privacy"
mod9.tidy$applicants<- "All applicants"

mod9b <- lm(activist ~ 
              female + voteprop, 
            data = filter(genetic.matches, privacy==1 & femaleapplicant==1),
            weights = weight)
mod9b.tidy <- tidy(mod9b)
mod9b.tidy$description <- "Privacy"
mod9b.tidy$applicants <- "Female"


Coef <- bind_rows(mod1.tidy,mod2.tidy,mod3.tidy,mod4.tidy,mod5.tidy,mod6.tidy,mod7.tidy,mod8.tidy,mod9.tidy,
                  mod1b.tidy,mod2b.tidy,mod3b.tidy,mod4b.tidy,mod5b.tidy,mod6b.tidy,mod7b.tidy,mod8b.tidy,mod9b.tidy )

Coef$conf.low <- Coef$estimate - 1.96*abs(Coef$std.error)
Coef$conf.high <-Coef$estimate + 1.96*abs(Coef$std.error)

FemaleCoef <- filter(Coef,term=="female")

FemaleCoef$term<-FemaleCoef$description
FemaleCoef$model<-FemaleCoef$applicants
library(dotwhisker)
dwplot(FemaleCoef)


coefplot <- dwplot(FemaleCoef, 
                   # here are our regular aesthetics
                   dot_args = list(aes(colour = model, 
                                       shape = model)), 
                   size = 3) + 
  theme_bw() + 
  labs(title = "Effect of Female Judge on Probability of Violation", 
       x = "Marginal Effect with 95% CIs", 
       y = "Issue") +
  theme(plot.title = element_text(face="bold"),
        legend.position = "bottom",
        legend.background = element_rect(colour="grey80"),
        legend.title.align = .5) +
  scale_shape_discrete(name  ="Models", breaks = c(0, 1)) + # breaks assign shapes
  scale_colour_grey(start = .3, end = .7, name = "Models") # start/end for light/dark greys

png("Coeffigure1Jan4.png",  width = 6, height = 8, units = 'in', res = 300)
coefplot
dev.off()

#Based on full regression models


mod1 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear  , 
           data = genetic.matches,
           weights = weight)
summary(mod1)

mod1.tidy <- tidy(mod1)
mod1.tidy$description <- "All Judgments"
mod1.tidy$applicants<- "All applicants"

mod1b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, femaleapplicant==1),
            weights = weight)
mod1b.tidy <- tidy(mod1b)
mod1b.tidy$applicants<- "Female"
mod1b.tidy$description <- "All Judgments"

mod1c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, maleapplicant==1),
            weights = weight)
mod1c.tidy <- tidy(mod1c)
mod1c.tidy$applicants<- "Male"
mod1c.tidy$description <- "All Judgments"

mod2 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, physical==1),
           weights = weight)
mod2.tidy <- tidy(mod2)
mod2.tidy$description <- "Physical integrity rights"
mod2.tidy$applicants<- "All applicants"

mod2b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, physical==1 & femaleapplicant==1),
            weights = weight)
mod2b.tidy <- tidy(mod2b)
mod2b.tidy$applicants <- "Female"
mod2b.tidy$description <- "Physical integrity rights"

mod2c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, physical==1 & maleapplicant==1),
            weights = weight)
mod2c.tidy <- tidy(mod2c)
mod2c.tidy$applicants <- "Male"
mod2c.tidy$description <- "Physical integrity rights"

mod3 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, liberty==1),
           weights = weight)
mod3.tidy <- tidy(mod3)
mod3.tidy$description <- "Liberty"
mod3.tidy$applicants<- "All applicants"

mod3b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, liberty==1 & femaleapplicant==1),
            weights = weight)
mod3b.tidy <- tidy(mod3b)
mod3b.tidy$description <- "Liberty"
mod3b.tidy$applicants <- "Female"

mod3c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, liberty==1 & maleapplicant==1),
            weights = weight)
mod3c.tidy <- tidy(mod3c)
mod3c.tidy$description <- "Liberty"
mod3c.tidy$applicants <- "Male"

mod4 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, prisoner==1),
           weights = weight)
mod4.tidy <- tidy(mod4)
mod4.tidy$description <- "Imprisoned applicant"
mod4.tidy$applicants<- "All applicants"

mod4b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, prisoner==1  & femaleapplicant==1),
            weights = weight)
mod4b.tidy <- tidy(mod4b)
mod4b.tidy$applicants <- "Female"
mod4b.tidy$description <- "Imprisoned applicant"

mod4c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, prisoner==1  & maleapplicant==1),
            weights = weight)
mod4c.tidy <- tidy(mod4c)
mod4c.tidy$applicants <- "Male"
mod4c.tidy$description <- "Imprisoned applicant"

mod5 <- lm(activist ~ 
             voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches,property==1),
           weights = weight)
mod5.tidy <- tidy(mod5)
mod5.tidy$description <- "Property"
mod5.tidy$applicants<- "All applicants"

mod5b <- lm(activist ~ 
              voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches,property==1  & femaleapplicant==1),
            weights = weight)
mod5b.tidy <- tidy(mod5b)
mod5b.tidy$description <- "Property"
mod5b.tidy$applicants <- "Female"

mod5c <- lm(activist ~ 
              voteprop + female + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches,property==1  & maleapplicant==1),
            weights = weight)
mod5c.tidy <- tidy(mod5c)
mod5c.tidy$description <- "Property"
mod5c.tidy$applicants <- "Male"

mod6 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, speech==1),
           weights = weight)
mod6.tidy <- tidy(mod6)
mod6.tidy$description <- "Free Speech"
mod6.tidy$applicants<- "All applicants"


mod6b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, speech==1 & femaleapplicant==1),
            weights = weight)
mod6b.tidy <- tidy(mod6b)
mod6b.tidy$description <- "Free Speech"
mod6b.tidy$applicants <- "Female"

mod6c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, speech==1 & maleapplicant==1),
            weights = weight)
mod6c.tidy <- tidy(mod6c)
mod6c.tidy$description <- "Free Speech"
mod6c.tidy$applicants <- "Male"

mod7 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, fairtrial==1),
           weights = weight)
mod7.tidy <- tidy(mod7)
mod7.tidy$description <- "Fair Trial"
mod7.tidy$applicants<- "All applicants"

mod7b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, fairtrial==1 & femaleapplicant==1),
            weights = weight)
mod7b.tidy <- tidy(mod7b)
mod7b.tidy$description <- "Fair Trial"
mod7b.tidy$applicants <- "Female"

mod7c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, fairtrial==1 & maleapplicant==1),
            weights = weight)
mod7c.tidy <- tidy(mod7c)
mod7c.tidy$description <- "Fair Trial"
mod7c.tidy$applicants <- "Male"


mod8 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, discrimination==1),
           weights = weight)
mod8.tidy <- tidy(mod8)
mod8.tidy$description <- "Discrimination"
mod8.tidy$applicants<- "All applicants"

mod8b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, discrimination==1 & femaleapplicant==1),
            weights = weight)
mod8b.tidy <- tidy(mod8b)
mod8b.tidy$description <- "Discrimination"
mod8b.tidy$applicants <- "Female"

mod8c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, discrimination==1 & maleapplicant==1),
            weights = weight)
mod8c.tidy <- tidy(mod8c)
mod8c.tidy$description <- "Discrimination"
mod8c.tidy$applicants <- "Male"


mod9 <- lm(activist ~ 
             female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
             right + religious + nationalist + legor_uk + 
             legor_fr +appointmentyear, 
           data = filter(genetic.matches, privacy==1),
           weights = weight)
mod9.tidy <- tidy(mod9)
mod9.tidy$description <- "Privacy"
mod9.tidy$applicants<- "All applicants"

mod9b <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, privacy==1 & femaleapplicant==1),
            weights = weight)
mod9b.tidy <- tidy(mod9b)
mod9b.tidy$description <- "Privacy"
mod9b.tidy$applicants <- "Female"

mod9c <- lm(activist ~ 
              female + voteprop + v2x_gender + v2x_liberal + diplomat + left + 
              right + religious + nationalist + legor_uk + 
              legor_fr +appointmentyear, 
            data = filter(genetic.matches, privacy==1 & maleapplicant==1),
            weights = weight)
mod9c.tidy <- tidy(mod9c)
mod9c.tidy$description <- "Privacy"
mod9c.tidy$applicants <- "Male"


Coef <- bind_rows(mod1.tidy,mod2.tidy,mod3.tidy,mod4.tidy,mod5.tidy,mod6.tidy,mod7.tidy,mod8.tidy,mod9.tidy,
                  mod1b.tidy,mod2b.tidy,mod3b.tidy,mod4b.tidy,mod5b.tidy,mod6b.tidy,mod7b.tidy,mod8b.tidy,mod9b.tidy,
                  mod1c.tidy,mod2c.tidy,mod3c.tidy,mod4c.tidy,mod5c.tidy,mod6c.tidy,mod7c.tidy,mod8c.tidy,mod9c.tidy)

Coef$conf.low <- Coef$estimate - 1.96*abs(Coef$std.error)
Coef$conf.high <-Coef$estimate + 1.96*abs(Coef$std.error)

write.csv(Coef,"CoefficientsGeneticMatchingJan4MB.csv")

FemaleCoef <- filter(Coef,term=="female")

FemaleCoef$term<-FemaleCoef$description
FemaleCoef$model<-FemaleCoef$applicants
library(dotwhisker)
dwplot(FemaleCoef)

Coef$model<-Coef$description


coefplot <- dwplot(FemaleCoef, 
                   # here are our regular aesthetics
                   dot_args = list(aes(colour = model, 
                                       shape = model)), 
                   size = 3) + 
  theme_light() + scale_colour_manual(values=c("black", "darkgray","gray"))+
  scale_size_manual(values=c(8,8,8)) +
  labs(title = "Genetic Matching", 
       x = "Marginal Effect with 95% CIs", 
       y = "Issue") +
  theme(plot.title = element_text(face="bold"),
        legend.position = "bottom",
        legend.background = element_rect(colour="grey80"),
        legend.title.align = .5) + geom_vline(xintercept = 0)
  

coefplot

png("Coeffigure2Jan4final.png",  width = 6, height = 8, units = 'in', res = 300)
coefplot
dev.off()
```

